
#include "easyview.h"

Easyview::Easyview()
{
    //创建标题和图标
    this->resize(2000,1250);
    this->setWindowTitle("捕鱼达人");
    this->setWindowIcon(QIcon("img/icon"));


    //创建视图场景
    scene=new QGraphicsScene;
    scene->setSceneRect(0,0,this->width()-2,this->height()-2);
    this->setScene(scene);


    //设置鼠标跟随
    this->setMouseTracking(true);


    //创建背景图
    this->setBackgroundBrush(QBrush(QPixmap("img/background3")));


    //创建大炮
    this->gun=new Gun("img/gun",scene);
    gun->setPos(this->width()/2,this->height());


    //创建鱼

    //从左向右动的鱼
    this->fish1=new Fish("img/leftfish/fish",scene);
    fish1->setPos(-200-rand()%500,rand()%400);


    this->fish2=new Fish("img/leftfish/fish2",scene);
    fish2->setPos(-200-rand()%500,200+rand()%400);


    this->fish3=new Fish("img/leftfish/fish3",scene);
    fish3->setPos(-200-rand()%500,400+rand()%400);


    this->fish4=new Fish("img/leftfish/fish4",scene);
    fish4->setPos(-200-rand()%500,600+rand()%400);


    this->fish5=new Fish("img/leftfish/fish5",scene);
    fish5->setPos(-200-rand()%500,800+rand()%400);


    //从右向左动的鱼
    this->fish_1=new Fish2("img/rightfish/fish1",scene);
    fish1->setPos(2200,100+rand()%400);


    this->fish_2=new Fish2("img/rightfish/fish2",scene);
    fish1->setPos(2200,300+rand()%400);


    this->fish_3=new Fish2("img/rightfish/fish3",scene);
    fish1->setPos(2200,500+rand()%400);


    this->fish_4=new Fish2("img/rightfish/fish4",scene);
    fish1->setPos(2200,700+rand()%400);


    this->fish_5=new Fish2("img/rightfish/fish5",scene);
    fish1->setPos(2200,900+rand()%400);



    //创建定时器
    timer=new QTimer;
    connect(timer,SIGNAL(timeout()),scene,SLOT(advance()));
    connect(timer,SIGNAL(timeout()),scene,SLOT(Fish2::advance()));
    connect(timer,SIGNAL(timeout()),this,SLOT(this->clickback()));
    timer->start(100);
}


void Easyview::resizeEvent(QResizeEvent *event)//调整游戏窗口大小
{

    this->setBackgroundBrush(QBrush(QPixmap("img/background3").scaled(event->size())));
}


void Easyview::mouseMoveEvent(QMouseEvent*event)//设置大炮鼠标跟随鼠标位置旋转
{
    QPoint p=event->pos();
    QLineF linef(this->width()/2,this->height(),p.x(),p.y());
    gun->setRotation(90-linef.angle());
}


void Easyview::mousePressEvent(QMouseEvent*event)
{

    QPoint p=event->pos();
    QLineF linef(this->width()/2,this->height(),p.x(),p.y());
    Boom * boom=new Boom("img/boom",scene,linef.angle());

}
